import { getChannels, getAllChannels } from '@/api/channels'
const state = {
  userChannels: [],
  AllChannels: []
}
const mutations = {
  setUserChannel(state, value) {
    state.userChannels = value
  },
  setAllChannel(state, value) {
    state.AllChannels = value
  },
  // 添加可选项
  addChannel(state, value) {
    state.userChannels.push(value)
  },
  // 删除
  delChannel(state, value) {
    state.userChannels = state.userChannels.filter(item => item.id !== value.id)
  }
}
const actions = {
  // 获取用户频道列表
  async getUserChannel(context) {
    const res = await getChannels()
    // state.userChannels = res.data.channels
    context.commit('setUserChannel', res.data.channels)
    console.log(res.data.channels)
  },
  // 获取所有频道列表
  async getAllChannels(context) {
    const res = await getAllChannels()
    context.commit('setAllChannel', res.data.channels)
  }
}
const getters = {
  optionChannels(state) {
    return state.AllChannels.filter(
      item => !state.userChannels.some(res => res.id === item.id)
    )
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
